package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLListDoing;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.mall.lib.dto.MallFundFlowDto;
import com.rockcent.mall.lib.dto.MerchantFundDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by DK on 16/11/19.
 */
@Repository
public class MerchantFundRepositoryImpl implements MerchantFundRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(MerchantFundRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public PageResult<MerchantFundDto> findMallMerchantFundFlowPage(Long mallId, String merchantName, String merchantAccount, PageResult pageResult) {
        SQLPageDoing<MerchantFundDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from merchant_fund mf left join merchant m on mf.merchant_id = m.id left join auth_user au on m.account_id = au.id where 1=1 ");
            if (StringUtils.isNotBlank(merchantName)) {
                sql.append(" and m.name like :merchantName");
                params.put("merchantName", "%" + merchantName + "%");
            }
            if (StringUtils.isNotBlank(merchantAccount)) {
                sql.append(" and au.username like :merchantAccount");
                params.put("merchantAccount", "%" + merchantAccount + "%");
            }
            sql.append(" and mf.mall_id = :mallId");
            params.put("mallId", mallId);
            sql.append(" order by mf.date_created desc");
            fields.append("mf.*, m.name merchantName");
        };
        return doing.go(pageResult, MerchantFundDto.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public List<MerchantFundDto> findMallMerchantFundFlowExport(Long mallId, String merchantName, String merchantAccount) {
        SQLListDoing<MerchantFundDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from merchant_fund mf left join merchant m on mf.merchant_id = m.id left join auth_user au on m.account_id = au.id where 1=1 ");
            if (StringUtils.isNotBlank(merchantAccount)) {
                sql.append(" and au.username like :merchantAccount");
                params.put("merchantAccount", "%" + merchantAccount + "%");
            }
            if (StringUtils.isNotBlank(merchantName)) {
                sql.append(" and m.name like :merchantName");
                params.put("merchantName", "%" + merchantName + "%");
            }
            sql.append(" and mf.mall_id = :mallId");
            params.put("mallId", mallId);
            sql.append(" order by mf.date_created desc");
            fields.append("mf.*, m.name merchantName");
        };
        return doing.go(MerchantFundDto.class, objectMapper, jdbcTemplate, log);
    }
}
